package com.dlc.yunguang.adapter;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.yunguang.net.bean.MoveProductBean;
import com.dlc.yunguang.R;

import java.util.ArrayList;

/**
 * Created by John on 2017/7/20.
 */
public class MoveProductAdapter<T extends MoveProductBean.DataBean> extends BaseRecyclerAdapter<T> {
    private ArrayList<MoveProductBean.DataBean> selectData = new ArrayList<>();
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_move_product;
    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {

        T item = getItem(position);
        holder.setText(R.id.tv_name, item.getTitle());
        holder.setText(R.id.tv_amount, String.valueOf(item.getCount()));
        holder.getView(R.id.check).setEnabled(item.isSelected());
    }

    public void selectIt(int position) {
        T item = getItem(position);
        item.setSelected(!item.isSelected());
        notifyItemChanged(position, "");
    }

    public void switchSelectAll() {

        if (mData.size() < 1) {
            return;
        }

        // 有未选择的
        boolean hasUnSelected = false;

        // 遍历所有项，遇到有未选择的，设置并跳出循环
        for (T t : mData) {
            hasUnSelected = !t.isSelected();
            if (hasUnSelected) {
                break;
            }
        }
        // 如果有未选择的，则全选，否则全部取消选择
        for (T t : mData) {
            t.setSelected(hasUnSelected);
        }
        notifyDataSetChanged();
    }

    public ArrayList<MoveProductBean.DataBean> getSelectData(){
        // 有未选择的
        boolean hasUnSelected = false;

        // 遍历所有项，遇到有未选择的，设置并跳出循环
        for (T t : mData) {
            hasUnSelected = t.isSelected();
            if (hasUnSelected) {
                selectData.add(t);
            }
        }
        return selectData;
    }
}
